package ${object.javaPackagePath}controller.${object.moduleName};

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import ${object.javaPackagePath}core.controller.BaseController;
import ${object.javaPackagePath}entity.${object.moduleName}.${table.className};
import ${object.javaPackagePath}service.${object.moduleName}.${table.className}Service;

 <#if table.cnName??>
/**
 * @describe ${object.describe} 
 * @author ${object.author}
 * @date ${object.createTime}
 * @version ${object.version}
 */
</#if>
@Controller("${table.varName}Controller")
@RequestMapping("/${table.varName}")
public class ${table.className}Controller extends BaseController<${table.className}> {

    private static final String REDIRECT_QUERY = "redirect:/${table.varName}/query.action";
    
    <#list table.pkCloums as pk>
	@Autowired
    private ${table.className}Service ${table.varName}Service;
    
    /**
     * 查询模块列表 /${table.varName}/search${table.className}
     */
    @RequestMapping(value="/search" , method={RequestMethod.GET, RequestMethod.POST})
    public ModelAndView search${table.className}(HttpServletRequest request, ${table.className} ${table.varName}) throws Exception {

        PageInfo<${table.className}> pageInfo = ${table.varName}Service.selectPage(getPageInfo(request, ${table.varName}));
        return new ModelAndView("${table.varName}/list").
                addObject("pageInfo", pageInfo);
    }
    
    /**
     * 根据主键加载用户信息 /${table.varName}/loadBy${pk.methodName}/{${pk.propertyName}}
     */
    @RequestMapping(value="/loadBy${pk.methodName}/{${pk.propertyName}}" , method=RequestMethod.GET)
    public ModelAndView load${table.className}(@PathVariable(value="${pk.propertyName}") ${pk.javaType?substring(10)} ${pk.propertyName}) throws Exception {
        if(${pk.propertyName} == null){
            return new ModelAndView("${table.varName}/add");
        }else{
            return new ModelAndView("${table.varName}/edit")
            .addObject("${table.varName}", ${table.varName}Service.selectByPrimaryKey(${pk.propertyName}));
        }
    }
    
     /**
     * 保存或更新功能 /${table.varName}/save
     */
    @RequestMapping(value="/save", method=RequestMethod.POST)
    public String save${table.className}(${table.className} ${table.varName}) throws Exception {
        if(${table.varName} == null) {
            throw new RuntimeException("参数异常");
        }
        if(${table.varName}Service.save(${table.varName}) != 1) {
            throw new RuntimeException("保存失败！");
        }
        return REDIRECT_QUERY;
    }
    
     /**
     * 根据主键删除数据 /${table.varName}/deleteBy${pk.methodName}/{${pk.propertyName}}
     */
    @RequestMapping(value="/deleteBy${pk.methodName}/{${pk.propertyName}}", method=RequestMethod.GET)
    public String delete${table.className}(@PathVariable(value="${pk.propertyName}") ${pk.javaType?substring(10)} ${pk.propertyName}) throws Exception {
        if(${pk.propertyName} == null) {
            throw new RuntimeException("参数异常");
        }
        if (${table.varName}Service.deleteByPrimaryKey(${pk.propertyName}) != 1){
            throw new RuntimeException("删除失败");
        }
        return REDIRECT_QUERY;
    }
    </#list>
}
